﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using System.Linq;

namespace HCMServices.DAO
{
    [Table(Name = "TUKHOAQUANHUYEN")]
    public class KeyWardDAO
    {
        private MyDataContext db;

        public KeyWardDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        [Column(Name = "MaTuKhoaQuanHuyen", IsDbGenerated = true, IsPrimaryKey = true, DbType = "int")]
        public int MaTuKhoaQuanHuyen { get; set; }

        [Column(Name = "TuKhoaQuanHuyen", DbType = "nvarchar(64)")]
        public string TuKhoaQuanHuyen { get; set; }

        [Column(Name = "MaQuanHuyen", DbType = "int")]
        public int MaQuanHuyen { get; set; }

        // what purpose of this function
        public List<KeyWardDTO> Search(string strInput)
        {
            List<KeyWardDTO> lstTuKhoaPhuongDTO = new List<KeyWardDTO>();

            IList<TUKHOAPHUONG> query = (from TUKHOAPHUONG tkp in db.KeyWard
                                         where tkp.TuKhoaPhuong1 == strInput
                                         select tkp).ToList();

            foreach (TUKHOAPHUONG tkp in query)
            {
                if (tkp.MaPhuong != null)
                {
                    KeyWardDTO temp = new KeyWardDTO();
                    temp.KeyWard = tkp.TuKhoaPhuong1;
                    temp.KeyWardId = tkp.MaTuKhoaPhuong;
                    temp.WardId = (int)tkp.MaPhuong;                // Take careful !! tkp.MaPhuong maybe null.

                    lstTuKhoaPhuongDTO.Add(temp);
                }
            }

            return lstTuKhoaPhuongDTO;
        }

        public Dictionary<string, string> GetDictionaryOfKey()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            var query = (from tkp in db.KeyWard
                         from phuong in db.Ward
                         where tkp.MaPhuong == phuong.MaPhuong
                         select new { tkp.TuKhoaPhuong1, phuong.TenPhuong }).ToList();

            foreach (var t in query)
            {
                if (t.TuKhoaPhuong1 != null && !result.ContainsKey(t.TuKhoaPhuong1))
                    result.Add(t.TuKhoaPhuong1, t.TenPhuong);
            }

            return result;
        }
    }
}
